package com.cskaoyan.day17;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DeleteDirectory {
    public static List<String> s = new ArrayList<>();

    public static void main(String[] args) {
        deleteDirectory("E:/Constructor", 0);
        System.out.println();
        s.add("E:/test目录已被删除！");
        for (int i = s.size()-1; i >=0 ; i--) {
            System.out.println(s.get(i));
        }
    }

    public static void deleteDirectory(String str, int deep) {
        File[] files = new File(str).listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            StringBuilder sb = new StringBuilder();
            if (file.isFile()) {
                file.delete();
                for (int i = 0; i < deep; i++) {
                    sb.append("\t");
                }
                sb.append("文件 " + file.getName() + "已经被删除");
                s.add(sb.toString());
                continue;
            }
            if (file.isDirectory()) {
                if (file.listFiles() != null) {
                    deleteDirectory(file.getAbsolutePath(), deep + 1);
                }
            }
            file.delete();
            for (int i = 0; i < deep; i++) {
                sb.append("\t");
            }
            sb.append("目录 " + file.getName() + "已经被删除");
            s.add(sb.toString());
        }
    }
}

